#encoding:utf-8
#!/usr/bin/env python

from flask import Flask, render_template
from flask_socketio import SocketIO, send, emit

import random


app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'

socketio = SocketIO(app)


@app.route('/')
def index():
    return render_template('index.html')


@socketio.on('client_event')
def client_msg(msg):
    max = int(msg['data'])
    for i in range(max):
        socketio.sleep(1)
        t = random_int_list(1, 100, 10)
        # socketio.emit('server_response',
        #               {'data': t},
        #               namespace='/test_conn')

        emit('server_response', {'data': t})


@socketio.on('connect_event')
def connected_msg(msg):
    emit('server_response', {'data': msg['data']})


def random_int_list(start, stop, length):
    start, stop = (int(start), int(stop)) if start <= stop else (int(stop), int(start))
    length = int(abs(length)) if length else 0
    random_list = []
    for i in range(length):
        random_list.append(random.randint(start, stop))
    return random_list

if __name__ == '__main__':
    socketio.run(app, debug=True)
